//
//  AQ.swift
//  test
//
//  Created by 曹亚索 on 2021/5/14.
//

import Foundation
import Alamofire
import SwiftyJSON
class AQ{
    //MARK:数据源
    var array = [0,1,2,3]
    var nums = [Int]()
    var alert : UIAlertController!
    
    var wronganswerarray = [String]()
    let RenWuArray = ["金钟国","李光沭","刘在锡","宋智孝","HAHA","姜GARY","王鼻子"]
    let CityArray = ["北京","天津","上海","重庆","石河子","阿拉尔市","图木舒克","五家渠","哈密","吐鲁番","阿克苏","喀什","和田","伊宁","塔城","阿勒泰","奎屯","博乐","昌吉","阜康","库尔勒","阿图什","乌苏","银川","石嘴山","吴忠","固原","中卫","南宁","柳州","桂林","梧州","北海","崇左","来宾","贺州","玉林","百色","河池","钦州","防城港","贵港","岑溪","凭祥","合山","北流","宜州","东兴","桂平","哈尔滨","大庆","齐齐哈尔","佳木斯","鸡","鹤岗","双鸭山","牡丹江","伊春","七台河","黑河","绥化","五常","双城","尚志","纳河","虎林","密山","铁力","同江","富锦","绥芬河","海林","宁安","穆林","北安","五大连池","肇东","海伦","安达"]
    let NumberArray = ["6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]
    
    let wrongAnswerArray = ["6","12","18"]
    var WAAPick = [String]()
    func AQPrepare(){
        internetRequst()
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1) {
            self.randomArray()
        }
    }
    
    func arraySDK(){
        let ArraySDK = [RenWuArray,CityArray,NumberArray]
        for array in ArraySDK{
            wronganswerarray.append(contentsOf: array)
        }
    }
    
    //MARK:网络请求模块
    func internetRequst(){
        if AQSwitch == true{
            AF.request("https://api.tianapi.com/txapi/wenda/index?key=93a8ea6c3343dbabddd30603816982f3").response { response in
                let json = JSON(response.value!!)
                question = json["newslist"][0]["quest"].stringValue
                currentResult = json["newslist"][0]["result"].stringValue
            }
        }else{
            let num = Int.random(in: 0...qbArray.count-1)
            question =  qbArray[num].question
            currentResult = qbArray[num].answer
        }
        
    }
    
    //MARK:随机数组模块
    func randomArray(){
        WAAPick = arrayPicker()
        //按错误答案量建立数字数组
        let wrongAnswerCount = WAAPick.count
        for i in 0...wrongAnswerCount-1{
            nums.append(i)
        }
        //生成答案数组
        let answerArray = [String(currentResult),WAAPick[numsRandomMan()],WAAPick[numsRandomMan()],WAAPick[numsRandomMan()]]
        //答案数组位置随机
        resultArray = [String(answerArray[arrayRandomMan()]),String(answerArray[arrayRandomMan()]),String(answerArray[arrayRandomMan()]),String(answerArray[arrayRandomMan()])]
    }
    //MARK:弹窗模块
    func showAlert(sender:UIViewController){
        //建立alert
        alert = UIAlertController(title: "\(String(question))", message: "请选择正确答案，回答正确+10", preferredStyle: .alert)
        //添加到alert里面
        for result in resultArray{
            alert.addAction(UIAlertAction(title: result, style: .default, handler: { (default) in
                //判断答案控制流
                print(result,String(currentResult))
                if result == currentResult{
                    print("right")
                }else{
                    print("wrong")
                }
            }))
        }
        sender.present(alert, animated: true, completion: nil)
    }
    //MARK:extension随机函数模块
    func arrayRandomMan() -> Int! {
            if !array.isEmpty {
                //随机返回一个数，同时从数组里删除
                let index = Int(arc4random_uniform(UInt32(array.count)))
                let a = array.remove(at: index)
                return a
            }
            else {
                //所有值都随机完则返回nil
                return nil
            }
    }
    func numsRandomMan() -> Int! {
            if !nums.isEmpty {
                //随机返回一个数，同时从数组里删除
                let index = Int(arc4random_uniform(UInt32(nums.count)))
                let a = nums.remove(at: index)
                return a
            }
            else {
                //所有值都随机完则返回nil
                return nil
            }
    }
    func arrayPicker()->Array<String>{
//        if AQSwitch == true{
            arraySDK()
            return wronganswerarray
//        }else{
//            return wrongAnswerArray
//        }
    }
}
